/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.application.user.assembler;

import com.lap.auth.application.user.command.CreateUserCmd;
import com.lap.auth.application.user.command.UpdateUserCmd;
import com.lap.auth.domain.user.User;
import com.lap.auth.domain.user.valueobject.Status;
import com.lap.auth.domain.user.valueobject.UserType;
import com.lap.auth.shared.tools.BaseConverter;
import com.lap.framework.common.tool.EnumUtil;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface UserAssembler extends BaseConverter {

  @Mapping(target = "id", ignore = true)
  @Mapping(target = "version", constant = "1")
  @Mapping(target = "password", source = "encodedPassword")
  @Mapping(target = "type", expression = "java(initType())")
  @Mapping(target = "orgPath", source = "cmd.orgPaths")
  @Mapping(target = "orgId", source = "cmd.orgPaths")
  @Mapping(target = "status", source = "cmd.status")
  User toDomain(String encodedPassword, CreateUserCmd cmd);

  @Mapping(target = "userName", ignore = true)
  @Mapping(target = "password", source = "encodedPassword")
  @Mapping(target = "orgPath", source = "cmd.orgPaths")
  @Mapping(target = "orgId", source = "cmd.orgPaths")
  @Mapping(target = "status", source = "cmd.status")
  User toDomain(Integer id, String encodedPassword, UserType type, UpdateUserCmd cmd);

  default UserType initType() {
    return UserType.NORMAL;
  }

  default Status codeToStatus(Integer code) {
    return EnumUtil.fromCode(Status.class, code);
  }
}
